package com.base.core.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class WebLog {
    private Logger logger = LoggerFactory.getLogger(WebLog.class);

    @Pointcut("@annotation(com.base.core.aop.annotation.ApiLog)")
    public void webLog() {

    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (HttpServletRequest)requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        logger.info("Url：{}", request.getRequestURL().toString());
        logger.info("HTTP_METHORD：{}", request.getMethod());
        logger.info("IP：{}", request.getRemoteAddr());
        logger.info("Class_method：{}", joinPoint.getSignature().getDeclaringTypeName() +"." +joinPoint.getSignature().getName());
        logger.info("参数：{}", Arrays.toString(joinPoint.getArgs()));
    }
}
